package net.bwie.jtp.dim.trade.job;

import net.bwie.jtp.dim.trade.function.DimDataFilterFunction;
import net.bwie.jtp.dim.trade.sink.DimHbaseSinkFunction;
import net.bwie.jtp.dim.trade.function.HbaseDimMapFunction;
import net.bwie.realtime.jtp.common.utils.KafkaUtil;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.HashMap;
import java.util.HashSet;

public class RealtimeSyncDataDimJob {
    public static void main(String[] args) throws Exception {
        // 1. 创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 2. 读取数据
        DataStream<String> dataStream = KafkaUtil.consumerKafka(env, "topic-db");
        // 3. 数据转换
        DataStream<String> dimStream = handle(dataStream);

        //  4. 写入数据
        dimStream.addSink(new DimHbaseSinkFunction());
//        dataStream.print();
        env.execute("RealtimeAsyncDataDimJob");
    }

    private static DataStream<String> handle(DataStream<String> dataStream) {
        HashMap<String, String> dimMap = new HashMap<String, String>() {{
            put("base_dic", "dic_code");
            put("base_province", "id");
            put("base_region", "id");
            put("base_category1", "id");
            put("base_category2", "id");
            put("base_category3", "id");
            put("sku_info", "id");
            put("spu_info", "id");
            put("user_info", "id");
            put("activity_info", "id");
            put("activity_rule", "id");
            put("coupon_info", "id");
        }};
        HashSet<String> dimSet = new HashSet<>(dimMap.keySet());
        // 过滤维表数据
        SingleOutputStreamOperator<String> asyncStream = dataStream.filter(new DimDataFilterFunction(dimSet));
        //添加字段

        System.out.print(asyncStream);
        SingleOutputStreamOperator<String> hbaseStream = asyncStream.map(new HbaseDimMapFunction(dimMap));
        return hbaseStream;
    }
}
